<?php
defined('o') || exit('404 not find file'); //防跳墙

require_once('model.class.php');

class templet extends model
{
	private $contents = ''; //模板目录
	private $array_model = array(); 
	// private $array_data = array();
	// private static $array_match=array('\{\w+\}','\{\w+[( +|\,|\$|\')\w+]+\}','\{list [^\{list|^end}]*.+? end\}');
	public function __construct($filelist)
	{
		$this->contents = $filelist;
	}

	public function set_modellist($filelist)
	{
		$this->contents = $filelist;
	}

	public function set_file($filename, $modefile)
	{
		$this->array_model["$filename"] = fileengine::file_get_string($this->contents . $modefile); 
		// regular::match_all('['. usersystem::array_get(self::$array_match,'|',false) .']',$this->array_model["$filename"],$this->array_data);
	}

	public function parse($filename)
	{
		echo $this->array_model[$filename];
	}

        public function get_file($filename) {
                return $this->array_model[$filename];
        }

	public function set_val($filename, $key, $val)
	{
		if (!empty($array_data))
		{
			return false;
		}
		else
		{
			$this->array_model["$filename"] = regular::replace_all('[\{' . $key . '\}]', $val, $this->array_model["$filename"]);
		}
	}

	public function set_list($filename, $keyword, $name, $array_list)
	{ 
		// 判断需替换的变量数组是否为空
		if (is_array($array_list))
		{ 
			// 根据提供的条件正则提取出配套的代码
			Regular::match_all("[\{list.*$keyword=\'$name\'.*\}[\s\S|/s/S]*?[\{/list\}]*?]", $this->array_model["$filename"], $temps);
			if (!empty($temps))
			{
				foreach ($temps as $tempd)
				{ 
					// 循环取出模板代码
					foreach($tempd as $temp)
					{ 
						// 取list限制显示数量 赋值 给 $count
						$count = self::get_key_int($temp, 'count', '='); 
						// 匹配模板代码中代换的原文
						Regular::match("[\}[\s\S|/s/S]*?\{/list\}]", $temp, $keyword1); 
						// 将匹配到的原文去除不需要的格式文本
						$outtext = trim(trim($keyword1, '{/list}'), '}'); 
						// 复制一份代码用于克隆
						$model_val = $outtext; 
						// 循环用于替换的关键字数组集合
						// 判断如果已经是数组最后一个即将用于克隆的文本赋空,防止继续克隆
						foreach($array_list as $temp1)
						{
							if ($array_list[count($array_list)-1] === $temp1)
							{
								$model_val = '';
							}
							foreach($temp1 as $keys => $vals)
							{
								$outtext = str_replace("{{$keys}}", $vals, $outtext) ;
							} 
							// 计次递减
							$count--; 
							// 判断是否超过list模板限制克隆的数量 是则跳出循环 否则将继续克隆
							if ($count < 1)
							{
								break;
							}
							else
							{
								$outtext .= $model_val;
							}
						} 
						// 将替换好内部变量的代码替换进原文
						$this->array_model["$filename"] = str_replace($temp, $outtext, $this->array_model["$filename"]);
					}
				}
				return true;
			}
		}
		return false;
	}

	static private function get_key_int($str, $key, $keyword)
	{
		$counts = Regular::match_all("[$key$keyword\'\d+\']", $str, $data);
		if ($counts)
		{
			$counts = Regular::match_all("[\d+]", $data[0][0], $data1);
			if ($counts)
			{
				return $data1[0][0];
			}
		}
		return 0;
	}

	static private function get_key_string($str, $key, $keyword)
	{
		$counts = Regular::match_all("[$key$keyword\'\w+\']", $str, $data);
		if ($counts)
		{
			$counts = Regular::match_all("[\'.+\']", $data[0][0], $data1);
			if ($counts)
			{
				return trim($data1[0][0], '\'');
			}
		}
		return '';
	}
}

?>